import { reactive, ref } from "vue"
import { defineStore } from "pinia"
import axios from "axios" // 整体暴露
import { nanoid } from "nanoid" // 分别暴露,用{}

// const uri = 'https://api.uomg.com/api/rand.qinghua?format=json'
// const uri = 'https://api.lovelive.tools/api/SweetNothings'

/*
 这是选项式写法
export const useTalkStore = defineStore("talk", {
  actions: {
    async getATalk() {
      let result = await axios.get(
        "http://localhost:5173/api/api/SweetNothings"
      )
      const obj = { id: nanoid(), title: result.data }
      this.talkList.unshift(obj)
    },
  },
  state() {
    return {
      talkList: JSON.parse(localStorage.getItem("talkList") || "[]"),
    }
  },
})
*/

// 组合式写法
export const useTalkStore = defineStore("talk", () => {
  // 这里相当于state
  const talkList = reactive(
    JSON.parse(localStorage.getItem("talkList") || "[]")
  )

  // 相当于actions
  async function getATalk() {
    // let result = await axios.get("http://localhost:5173/api/api/SweetNothings")
    // const obj = { id: nanoid(), title: result.data }
    let {
      data: { content: title },
    } = await axios.get(
      "http://localhost:5173/api/api/rand.qinghua?format=json"
    )
    const obj = { id: nanoid(), title }
    talkList.unshift(obj)
  }
  return { talkList, getATalk }
})
